package com.bit.forum.service;

import com.bit.forum.common.AppResult;
import com.bit.forum.common.ResultCode;
import com.bit.forum.dao.UserMapper;
import com.bit.forum.exception.ApplicationException;
import com.bit.forum.model.User;
import com.bit.forum.utils.MD5Util;
import com.bit.forum.utils.StringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class UserServiceImpl implements IUserService{

    @Resource
    private UserMapper userMapper;

    @Override
    public void createNormalUser(User user) {
        //非空校验
        if(user==null|| StringUtil.isEmpty(user.getUsername())||StringUtil.isEmpty(user.getPassword())
        ||StringUtil.isEmpty(user.getNickname())||StringUtil.isEmpty(user.getSalt())){
            //打印日志
            log.error(ResultCode.ERROR_IS_NULL.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //查询用户是否存在
        User existsUser=userMapper.selectByUserName(user.getUsername());
        if(existsUser!=null){
            log.info(ResultCode.AILED_USER_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.AILED_USER_EXISTS));
        }
        //新增用户流程
        user.setGender((byte) 2);
        user.setDeleteState((byte) 0);
        user.setArticleCount(0);
        user.setIsAdmin((byte) 0);
        user.setState((byte) 0);
        //当前日期
        Date data=new Date();
        user.setCreateTime(data);
        user.setUpdateTime(data);
        //写入数据库
        int row = userMapper.insertSelective(user);
        if(row!=1){
            log.info(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        //打印日志
        log.info("新增用户成功. username="+user.getUsername()+".");

    }

    @Override
    public User selectByUsername(String username) {
        if(StringUtil.isEmpty(username)){
            log.error(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        return userMapper.selectByUserName(username);
    }

    @Override
    public User login(String username, String password) {
        //非空校验
        User user=selectByUsername(username);
        if(user==null){
            log.error(ResultCode.FAILED_LOGIN.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }
        String encryptPassword= MD5Util.md5Salt(password,user.getSalt());
        if(!encryptPassword.equals(user.getPassword())){
            log.error(ResultCode.FAILED_LOGIN.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }
        return user;
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        if(id==null){
            log.error(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //非空校验
        if(id==null||id<=0){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        User user = userMapper.selectByPrimaryKey(id);
        if(user==null){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        User newUser=new User();
        newUser.setId(id);
        newUser.setArticleCount(user.getArticleCount()+1);
        int row = userMapper.updateByPrimaryKeySelective(newUser);
        if(row<=0){
            log.info(ResultCode.FAILED.toString()+"用户:"+user.getUsername()+"新增文章失败");
        }
        log.info(ResultCode.SUCCESS.toString()+"用户:"+user.getUsername()+"新增文章成功");
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //非空校验
        if(id==null||id<=0){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        User user = userMapper.selectByPrimaryKey(id);
        user.setArticleCount(user.getArticleCount()-1);
        if(user.getArticleCount()<0){
            user.setArticleCount(0);
        }
        int row = userMapper.updateByPrimaryKeySelective(user);
        if(row!=1){
            log.info(ResultCode.FAILED.toString()+"用户:"+user.getUsername()+"删除文章失败");
            throw new ApplicationException(AppResult.failed("删除文章失败"));
        }
        log.info(ResultCode.SUCCESS.toString()+"用户:"+user.getUsername()+"删除文章成功");
    }
}
